#include <linux/export.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/spi/spi.h>
#include "fbtft.h"

/*****************************************************************************
 *
 *   void (*write_reg)(struct fbtft_par *par, int len, ...);
 *
 *****************************************************************************/

#define define_fbtft_write_reg(func, type, modifier)                          \
void func(struct device *dev, struct fbtft_par *par, int len, ...)                                \
{                                                                             \
	va_list args;                                                         \
	int i, ret;                                                           \
	int offset = 0;                                                       \
	type *buf = (type *)par->buf;                                         \
									      \
	if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) {                    \
		va_start(args, len);                                          \
		for (i = 0; i < len; i++) {                                   \
			buf[i] = (type)va_arg(args, unsigned int);            \
		}                                                             \
		va_end(args);                                                 \
		fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, dev, type, buf, len, "%s: ", __func__);   \
	}                                                                     \
									      \
	va_start(args, len);                                                  \
									      \
	if (par->startbyte) {                                                 \
		*(u8 *)par->buf = par->startbyte;                             \
		buf = (type *)(par->buf + 1);                                 \
		offset = 1;                                                   \
	}                                                                     \
									      \
	*buf = modifier((type)va_arg(args, unsigned int));                    \
	if (par->gpio.dc != -1)                                               \
		gpio_set_value(par->gpio.dc, 0);                              \
	ret = fbtft_write_spi(dev,par, par->buf, sizeof(type)+offset);        \
	if (ret < 0) {                                                        \
		va_end(args);                                                 \
		dev_err(dev, "%s: write() failed and returned %d\n", __func__, ret); \
		return;                                                       \
	}                                                                     \
	len--;                                                                \
									      \
	if (par->startbyte)                                                   \
		*(u8 *)par->buf = par->startbyte | 0x2;                       \
									      \
	if (len) {                                                            \
		i = len;                                                      \
		while (i--) {                                                 \
			*buf++ = modifier((type)va_arg(args, unsigned int));  \
		}                                                             \
		if (par->gpio.dc != -1)                                       \
			gpio_set_value(par->gpio.dc, 1);                      \
		ret = fbtft_write_spi(dev,par, par->buf, len * (sizeof(type)+offset)); \
		if (ret < 0) {                                                \
			va_end(args);                                         \
			dev_err(dev, "%s: write() failed and returned %d\n", __func__, ret); \
			return;                                               \
		}                                                             \
	}                                                                     \
	va_end(args);                                                         \
}                                                                             \
EXPORT_SYMBOL(func);

define_fbtft_write_reg(fbtft_write_reg8_bus8, u8, )
//define_fbtft_write_reg(fbtft_write_reg16_bus8, u16, cpu_to_be16)
//define_fbtft_write_reg(fbtft_write_reg16_bus16, u16, )


/*****************************************************************************
 *
 *   int (*write_vmem)(struct fbtft_par *par);
 *
 *****************************************************************************/

/* 16 bit pixel over 8-bit databus */
int fbtft_write_vmem16_bus8(struct device *dev,struct fbtft_par *par, size_t offset, size_t len)
{
//	u16 *vmem16;
	u8 *txbuf = par->txbuf.buf;
	size_t remain;
	size_t to_copy;
	size_t tx_array_size;
	int i;
	int ret = 0;

//	fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s(offset=%zu, len=%zu)\n",
//		__func__, offset, len);

	/* remaining number of pixels to send */
	remain = len / 2;
//	vmem16 = (u16 *)(par->info->screen_buffer + offset);

//	if (par->gpio.dc != -1)
		gpio_set_value(par->gpio.dc, 1);

	/* number of pixels that fits in the transmit buffer */
	tx_array_size = par->txbuf.len / 3;

	while (remain) {
		/* number of pixels to copy in one iteration of the loop */
		to_copy = min(tx_array_size, remain);
//		dev_dbg(par->info->device, "    to_copy=%zu, remain=%zu\n",
//						to_copy, remain - to_copy);

		for (i = 0; i < to_copy; i++) {
//			u16 pixel = vmem16[i];
//			u16 b = pixel & 0x1f;
//			u16 g = (pixel & (0x3f << 5)) >> 5;
//			u16 r = (pixel & (0x1f << 11)) >> 11;

//			u8 r8 = (r & 0x1F) << 3;
//			u8 g8 = (g & 0x3F) << 2;
//			u8 b8 = (b & 0x1F) << 3;

			u8 r8 = (0x1f & 0x1F) << 3;
					u8 g8 = (0 & 0x3F) << 2;
					u8 b8 = (0 & 0x1F) << 3;

			txbuf[i * 3 + 0] = r8;
			txbuf[i * 3 + 1] = g8;
			txbuf[i * 3 + 2] = b8;
		}

//		vmem16 = vmem16 + to_copy;
		ret = fbtft_write_spi(dev,par, par->txbuf.buf, to_copy * 3);
		if (ret < 0)
			return ret;
		remain -= to_copy;
	}

	return ret;
}
EXPORT_SYMBOL(fbtft_write_vmem16_bus8);

